home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Easy Applet Builder v1.7 / _SETUP.1 / SoundPlayer.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-11-06  |  12.3 KB  |  520 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Button;
  4. import java.awt.Choice;
  5. import java.awt.Color;
  6. import java.awt.Component;
  7. import java.awt.Container;
  8. import java.awt.Font;
  9. import java.awt.FontMetrics;
  10. import java.awt.Graphics;
  11. import java.awt.Image;
  12. import java.awt.LayoutManager;
  13. import java.awt.MediaTracker;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.MouseEvent;
  17. import java.awt.event.MouseListener;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20. import java.net.URLEncoder;
  21. import java.util.EventObject;
  22.  
  23. public class SoundPlayer extends Applet implements Runnable, ActionListener, MouseListener {
  24.    // $FF: renamed from: wi int
  25.    private int field_0;
  26.    // $FF: renamed from: he int
  27.    private int field_1;
  28.    private Color backCol;
  29.    private int currentSound;
  30.    private Image backImage;
  31.    private String backImageName;
  32.    private Color backColor;
  33.    private AudioClip[] sounds;
  34.    private String[] soundNames;
  35.    private String[] soundFilesNames;
  36.    private Button buttonPlay;
  37.    private Button buttonStop;
  38.    private Button buttonPrev;
  39.    private Button buttonNext;
  40.    private String playLabel;
  41.    private String stopLabel;
  42.    private String prevLabel;
  43.    private String nextLabel;
  44.    private boolean[] isLoaded;
  45.    private Choice list;
  46.    private boolean loaded;
  47.    private Image buffer;
  48.    // $FF: renamed from: b java.awt.Graphics
  49.    private Graphics field_2;
  50.    private Image fond;
  51.    private Thread thread;
  52.    private int pause;
  53.    private boolean isPlaying = false;
  54.    // $FF: renamed from: fm java.awt.FontMetrics
  55.    private FontMetrics field_3;
  56.    private Image title;
  57.    private int titleWidth;
  58.    private int titleHeight;
  59.    private int space;
  60.    // $FF: renamed from: X int
  61.    private int field_4;
  62.    // $FF: renamed from: XX int
  63.    private int field_5;
  64.    private String prefix;
  65.    private int autoPlay;
  66.    private String statusBarText;
  67.  
  68.    public String getAppletInfo() {
  69.       return "Name: SoundPlayer\r\nAuthor: Taiji Software";
  70.    }
  71.  
  72.    public void register() {
  73.       try {
  74.          URL u = new URL("http://www.taijisoftware.com");
  75.          ((Applet)this).getAppletContext().showDocument(u, "_blank");
  76.       } catch (Exception e) {
  77.          System.out.println(e);
  78.          this.stop();
  79.       }
  80.    }
  81.  
  82.    public void init() {
  83.       String codeBase = null;
  84.  
  85.       try {
  86.          codeBase = (new URL(((Applet)this).getCodeBase().toString())).getHost();
  87.          System.out.println("Copyright 1999, 2001 Taiji Software(tm)\nYour domain name is : " + codeBase);
  88.          codeBase = codeBase.toUpperCase();
  89.       } catch (Exception var10) {
  90.       }
  91.  
  92.       if (!((Applet)this).getCodeBase().toString().toUpperCase().startsWith("FILE") || ((Applet)this).getParameter("debug") != null) {
  93.          String regCode = ((Applet)this).getParameter("registration_code");
  94.          if (regCode == null) {
  95.             regCode = ((Applet)this).getParameter("reg_domain");
  96.             if (regCode == null) {
  97.                this.register();
  98.             } else {
  99.                if (regCode.length() == codeBase.length() + 4 && !codeBase.startsWith("WWW.")) {
  100.                   codeBase = "WWW." + codeBase;
  101.                } else if (regCode.length() == codeBase.length() - 4 && codeBase.startsWith("WWW.")) {
  102.                   codeBase = codeBase.substring(4);
  103.                }
  104.  
  105.                char[] chars = new char[codeBase.length()];
  106.                codeBase.getChars(0, codeBase.length(), chars, 0);
  107.                String key = new String("haricot");
  108.                char[] chars2 = new char[key.length()];
  109.                key.getChars(0, key.length(), chars2, 0);
  110.  
  111.                for(int i = 0; i < codeBase.length(); ++i) {
  112.                   int j;
  113.                   if (i >= key.length()) {
  114.                      j = i - key.length() * (i / key.length());
  115.                   } else {
  116.                      j = i;
  117.                   }
  118.  
  119.                   chars[i] += chars2[j];
  120.                   chars[i] = (char)(chars[i] - chars[i] / 26 * 26 + 97);
  121.                }
  122.  
  123.                String res = new String(chars);
  124.                if (!res.equalsIgnoreCase(regCode)) {
  125.                   this.register();
  126.                }
  127.             }
  128.          } else if (!regCode.equals("settevercsedegnamiaj") && !regCode.equals("8078")) {
  129.             this.register();
  130.          }
  131.       }
  132.  
  133.       this.getParameters();
  134.       ((Container)this).setLayout((LayoutManager)null);
  135.       this.fond = ((Component)this).createImage(this.field_0, this.field_1);
  136.       this.buffer = ((Component)this).createImage(this.field_0, this.field_1);
  137.       this.field_2 = this.buffer.getGraphics();
  138.       if (this.backImageName == null) {
  139.          ((Component)this).setBackground(this.backColor);
  140.       } else {
  141.          this.backImage = ((Applet)this).getImage(((Applet)this).getCodeBase(), this.backImageName);
  142.          MediaTracker tracker = new MediaTracker(this);
  143.          tracker.addImage(this.backImage, 0);
  144.  
  145.          try {
  146.             tracker.waitForAll();
  147.             this.loaded = !tracker.isErrorAny();
  148.          } catch (Exception e) {
  149.             System.err.println(e);
  150.          }
  151.  
  152.          if (!this.loaded) {
  153.             System.out.println("problem while loading background image...");
  154.             this.stop();
  155.          }
  156.       }
  157.  
  158.       ((Component)this).repaint();
  159.       this.buttonPlay = new Button(this.playLabel);
  160.       this.buttonPlay.addActionListener(this);
  161.       ((Container)this).add(this.buttonPlay);
  162.       this.buttonStop = new Button(this.stopLabel);
  163.       this.buttonStop.addActionListener(this);
  164.       ((Container)this).add(this.buttonStop);
  165.       this.buttonNext = new Button(this.nextLabel);
  166.       this.buttonNext.addActionListener(this);
  167.       ((Container)this).add(this.buttonNext);
  168.       this.buttonPrev = new Button(this.prevLabel);
  169.       this.buttonPrev.addActionListener(this);
  170.       ((Container)this).add(this.buttonPrev);
  171.       int maxLength = this.buttonPlay.getPreferredSize().width + this.buttonStop.getPreferredSize().width + this.buttonNext.getPreferredSize().width + this.buttonPrev.getPreferredSize().width;
  172.       int space = (int)((float)(this.field_0 - maxLength) / 7.0F);
  173.       this.list = new Choice();
  174.       this.isLoaded = new boolean[this.soundNames.length];
  175.       this.sounds = new AudioClip[this.soundNames.length];
  176.  
  177.       for(int i = 0; i < this.soundNames.length; ++i) {
  178.          this.list.addItem(this.soundNames[i]);
  179.          this.isLoaded[i] = false;
  180.       }
  181.  
  182.       this.list.select(this.soundNames[0]);
  183.       ((Container)this).add(this.list);
  184.       this.buttonPrev.setBounds(2 * space, this.field_1 - this.list.getPreferredSize().height - this.buttonPrev.getPreferredSize().height - 20, this.buttonPrev.getPreferredSize().width, this.buttonPrev.getPreferredSize().height);
  185.       this.buttonPlay.setBounds(3 * space + this.buttonPrev.getPreferredSize().width, this.field_1 - this.list.getPreferredSize().height - this.buttonPrev.getPreferredSize().height - 20, this.buttonPlay.getPreferredSize().width, this.buttonPlay.getPreferredSize().height);
  186.       this.buttonStop.setBounds(4 * space + this.buttonPrev.getPreferredSize().width + this.buttonPlay.getPreferredSize().width, this.field_1 - this.list.getPreferredSize().height - this.buttonPrev.getPreferredSize().height - 20, this.buttonStop.getPreferredSize().width, this.buttonStop.getPreferredSize().height);
  187.       this.buttonNext.setBounds(5 * space + this.buttonPrev.getPreferredSize().width + this.buttonPlay.getPreferredSize().width + this.buttonStop.getPreferredSize().width, this.field_1 - this.list.getPreferredSize().height - this.buttonPrev.getPreferredSize().height - 20, this.buttonNext.getPreferredSize().width, this.buttonNext.getPreferredSize().height);
  188.       this.list.setBounds((this.field_0 - this.list.getPreferredSize().width) / 2, this.field_1 - this.list.getPreferredSize().height - 10, this.list.getPreferredSize().width, this.list.getPreferredSize().height);
  189.       this.initPaint();
  190.       if (this.autoPlay == 1) {
  191.          this.actionPerformed(new ActionEvent(this.buttonPlay, 1001, "play"));
  192.       }
  193.  
  194.       ((Component)this).repaint();
  195.    }
  196.  
  197.    public void getParameters() {
  198.       this.field_0 = ((Component)this).getSize().width;
  199.       this.field_1 = ((Component)this).getSize().height;
  200.       this.backImageName = ((Applet)this).getParameter("background_image");
  201.       if (this.getColor("background_color") != null) {
  202.          this.backColor = this.getColor("background_color");
  203.       }
  204.  
  205.       String s = ((Applet)this).getParameter("play_label");
  206.       if (s != null) {
  207.          this.playLabel = s;
  208.       }
  209.  
  210.       s = ((Applet)this).getParameter("stop_label");
  211.       if (s != null) {
  212.          this.stopLabel = s;
  213.       }
  214.  
  215.       s = ((Applet)this).getParameter("prev_label");
  216.       if (s != null) {
  217.          this.prevLabel = s;
  218.       }
  219.  
  220.       s = ((Applet)this).getParameter("next_label");
  221.       if (s != null) {
  222.          this.nextLabel = s;
  223.       }
  224.  
  225.       s = ((Applet)this).getParameter("prefix");
  226.       if (s != null) {
  227.          this.prefix = s;
  228.       }
  229.  
  230.       s = ((Applet)this).getParameter("pause");
  231.       if (s != null) {
  232.          this.pause = Integer.parseInt(s);
  233.       }
  234.  
  235.       s = ((Applet)this).getParameter("auto_play");
  236.       if (s != null) {
  237.          this.autoPlay = Integer.parseInt(s);
  238.       }
  239.  
  240.       s = ((Applet)this).getParameter("status_bar_text");
  241.       if (s != null) {
  242.          this.statusBarText = s;
  243.       }
  244.  
  245.       int n;
  246.       for(n = 1; ((Applet)this).getParameter("clip" + Integer.toString(n)) != null; ++n) {
  247.       }
  248.  
  249.       this.soundNames = new String[n - 1];
  250.       this.soundFilesNames = new String[n - 1];
  251.  
  252.       for(int i = 0; i < this.soundNames.length; ++i) {
  253.          this.soundFilesNames[i] = ((Applet)this).getParameter("clip" + Integer.toString(i + 1));
  254.          this.soundNames[i] = ((Applet)this).getParameter("clip_name" + Integer.toString(i + 1));
  255.          if (this.soundNames[i] == null) {
  256.             this.soundNames[i] = this.soundFilesNames[i];
  257.          }
  258.       }
  259.  
  260.    }
  261.  
  262.    private Color getColor(String param) {
  263.       String s = ((Applet)this).getParameter(param);
  264.       if (s != null) {
  265.          if (s.substring(0, 1).equals("#")) {
  266.             s = s.substring(1);
  267.             int i = Integer.parseInt(s, 16);
  268.             return new Color(i);
  269.          } else {
  270.             return null;
  271.          }
  272.       } else {
  273.          return null;
  274.       }
  275.    }
  276.  
  277.    public final void paint(Graphics g) {
  278.       if (this.backImageName == null || this.loaded) {
  279.          this.field_2.drawImage(this.fond, 0, 0, this);
  280.          this.field_2.drawImage(this.title, this.field_4, this.space, this);
  281.          this.field_2.drawImage(this.fond, 0, this.space, 10, this.space + this.titleHeight, 0, this.space, 10, this.space + this.titleHeight, this);
  282.          this.field_2.drawImage(this.fond, this.field_0 - 10, this.space, this.field_0, this.space + this.titleHeight, this.field_0 - 10, this.space, this.field_0, this.space + this.titleHeight, this);
  283.          g.drawImage(this.buffer, 0, 0, this);
  284.       }
  285.    }
  286.  
  287.    public final void update(Graphics g) {
  288.       this.paint(g);
  289.    }
  290.  
  291.    public void setTitle(String titre, boolean isPrefix) {
  292.       if (isPrefix) {
  293.          titre = this.prefix + " " + titre + " " + this.prefix;
  294.       }
  295.  
  296.       try {
  297.          Font f = new Font("TimesRoman", 1, this.titleHeight / 2);
  298.          this.field_2.setFont(f);
  299.       } catch (Exception var6) {
  300.          Font f = this.field_2.getFont();
  301.          this.field_2.setFont(new Font(f.getName(), f.getStyle(), this.titleHeight / 2));
  302.       }
  303.  
  304.       this.field_3 = this.field_2.getFontMetrics();
  305.       this.titleWidth = this.field_3.stringWidth(titre);
  306.       this.title = ((Component)this).createImage(this.titleWidth, this.titleHeight);
  307.       Graphics e = this.title.getGraphics();
  308.       e.setColor(Color.black);
  309.       e.fillRect(0, 0, this.titleWidth, this.titleHeight);
  310.  
  311.       try {
  312.          Font f = new Font("TimesRoman", 1, this.titleHeight / 2);
  313.          e.setFont(f);
  314.       } catch (Exception var5) {
  315.          Font f = this.field_2.getFont();
  316.          e.setFont(new Font(f.getName(), f.getStyle(), this.titleHeight / 2));
  317.       }
  318.  
  319.       e.setColor(Color.white);
  320.       e.drawString(titre, 0, this.field_3.getHeight());
  321.       this.field_4 = (this.field_0 - this.titleWidth) / 2;
  322.       e.dispose();
  323.    }
  324.  
  325.    public void run() {
  326.       Thread.currentThread().setPriority(4);
  327.  
  328.       while(true) {
  329.          this.field_4 -= 5;
  330.          if (this.field_4 < -this.titleWidth) {
  331.             this.field_4 = this.field_0;
  332.          }
  333.  
  334.          ((Component)this).repaint();
  335.  
  336.          try {
  337.             Thread.sleep((long)this.pause);
  338.          } catch (InterruptedException var1) {
  339.          }
  340.       }
  341.    }
  342.  
  343.    public void initPaint() {
  344.       Graphics e = this.fond.getGraphics();
  345.       if (this.backImageName != null) {
  346.          int wiIm = this.backImage.getWidth(this);
  347.          int heIm = this.backImage.getHeight(this);
  348.          int w = this.field_0 / wiIm + 1;
  349.          int h = this.field_1 / heIm + 1;
  350.  
  351.          for(int i = 1; i <= h; ++i) {
  352.             for(int j = 1; j <= w; ++j) {
  353.                e.drawImage(this.backImage, (j - 1) * wiIm, (i - 1) * heIm, this);
  354.             }
  355.          }
  356.       } else {
  357.          e.setColor(this.backColor);
  358.          e.fillRect(0, 0, this.field_0, this.field_1);
  359.       }
  360.  
  361.       e.setColor(Color.black);
  362.       this.space = this.buttonPrev.getLocation().y / 6;
  363.       e.fillRect(10, this.space, this.field_0 - 20, 4 * this.space);
  364.       e.dispose();
  365.       this.titleWidth = this.field_0 - 20;
  366.       this.titleHeight = 4 * this.space;
  367.    }
  368.  
  369.    public void actionPerformed(ActionEvent evt) {
  370.       if (((EventObject)evt).getSource() instanceof Button) {
  371.          Button b = (Button)((EventObject)evt).getSource();
  372.          if (b.getLabel().equals(this.playLabel)) {
  373.             if (this.isPlaying) {
  374.                this.sounds[this.currentSound].stop();
  375.             }
  376.  
  377.             this.currentSound = this.list.getSelectedIndex();
  378.             if (!this.isLoaded[this.currentSound]) {
  379.                this.sounds[this.currentSound] = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), this.soundFilesNames[this.currentSound]);
  380.                this.isLoaded[this.currentSound] = true;
  381.             }
  382.  
  383.             this.sounds[this.currentSound].play();
  384.             this.setTitle(this.soundNames[this.currentSound], true);
  385.             this.isPlaying = true;
  386.             return;
  387.          }
  388.  
  389.          if (b.getLabel().equals(this.stopLabel)) {
  390.             if (this.isPlaying) {
  391.                this.sounds[this.currentSound].stop();
  392.             }
  393.  
  394.             this.setTitle("   ", false);
  395.             this.isPlaying = false;
  396.             return;
  397.          }
  398.  
  399.          if (b.getLabel().equals(this.nextLabel)) {
  400.             if (this.isPlaying) {
  401.                this.sounds[this.currentSound].stop();
  402.             }
  403.  
  404.             ++this.currentSound;
  405.             if (this.currentSound >= this.soundNames.length) {
  406.                this.currentSound = 0;
  407.             }
  408.  
  409.             this.list.select(this.soundNames[this.currentSound]);
  410.             if (!this.isLoaded[this.currentSound]) {
  411.                this.sounds[this.currentSound] = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), this.soundFilesNames[this.currentSound]);
  412.                this.isLoaded[this.currentSound] = true;
  413.             }
  414.  
  415.             this.sounds[this.currentSound].play();
  416.             this.setTitle(this.soundNames[this.currentSound], true);
  417.             this.isPlaying = true;
  418.             return;
  419.          }
  420.  
  421.          if (b.getLabel().equals(this.prevLabel)) {
  422.             if (this.isPlaying) {
  423.                this.sounds[this.currentSound].stop();
  424.             }
  425.  
  426.             --this.currentSound;
  427.             if (this.currentSound < 0) {
  428.                this.currentSound = this.soundNames.length - 1;
  429.             }
  430.  
  431.             this.list.select(this.soundNames[this.currentSound]);
  432.             if (!this.isLoaded[this.currentSound]) {
  433.                this.sounds[this.currentSound] = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), this.soundFilesNames[this.currentSound]);
  434.                this.isLoaded[this.currentSound] = true;
  435.             }
  436.  
  437.             this.sounds[this.currentSound].play();
  438.             this.setTitle(this.soundNames[this.currentSound], true);
  439.             this.isPlaying = true;
  440.          }
  441.       }
  442.  
  443.    }
  444.  
  445.    public void start() {
  446.       if (this.thread == null) {
  447.          this.thread = new Thread(this);
  448.          this.thread.start();
  449.       }
  450.  
  451.    }
  452.  
  453.    public void stop() {
  454.       if (this.thread != null) {
  455.          this.thread = null;
  456.       }
  457.  
  458.       if (this.sounds[this.currentSound] != null) {
  459.          this.sounds[this.currentSound].stop();
  460.       }
  461.  
  462.    }
  463.  
  464.    public URL giveURL(String url) {
  465.       try {
  466.          if (url.toUpperCase().startsWith("HTTP")) {
  467.             return new URL(url);
  468.          } else if (url.toUpperCase().startsWith("FTP")) {
  469.             int p = url.indexOf(":");
  470.             int p2 = url.indexOf(":", p + 1);
  471.             if (p2 != -1) {
  472.                url = url.substring(0, p + 3) + URLEncoder.encode(url.substring(p + 3, url.length()));
  473.             }
  474.  
  475.             p = url.indexOf("%40");
  476.             if (p != -1) {
  477.                url = url.substring(0, p) + "@" + url.substring(p + 3, url.length());
  478.             }
  479.  
  480.             return new URL(url);
  481.          } else {
  482.             return new URL(((Applet)this).getCodeBase(), url);
  483.          }
  484.       } catch (MalformedURLException e) {
  485.          System.out.println(e);
  486.          return null;
  487.       }
  488.    }
  489.  
  490.    public SoundPlayer() {
  491.       this.backColor = Color.white;
  492.       this.pause = 200;
  493.       this.playLabel = "Play";
  494.       this.stopLabel = "Stop";
  495.       this.prevLabel = "Prev";
  496.       this.nextLabel = "Next";
  497.       this.prefix = "";
  498.       ((Component)this).addMouseListener(this);
  499.    }
  500.  
  501.    public void mouseClicked(MouseEvent e) {
  502.    }
  503.  
  504.    public void mouseEntered(MouseEvent e) {
  505.       if (this.statusBarText != null) {
  506.          ((Applet)this).showStatus(this.statusBarText);
  507.       }
  508.  
  509.    }
  510.  
  511.    public void mouseExited(MouseEvent e) {
  512.    }
  513.  
  514.    public void mousePressed(MouseEvent e) {
  515.    }
  516.  
  517.    public void mouseReleased(MouseEvent e) {
  518.    }
  519. }
  520.